﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Threading;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using VAdvantage.DataService;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using VAdvantage.Model;
namespace VAdvantage.DataBase
{
    public class DBQ
    {
       

        public  String ExecuteString(String sql)
        {

            string retValue = null;
            

            var client =  Utility.AEnv.GetDataServiceClient();

            try
            {
                client.GetDataXMLCompleted += ((sender, e) =>
                {
                        retValue = e.Result;
                    //m_autoResetEvent.Set();
                });
                client.GetDataXMLAsync(sql.ToString());
                //m_autoResetEvent.WaitOne();
            }
            catch (Exception e)
            {
                //m_autoResetEvent.Set();
            }
            return retValue;
        }


        public XElement xTable = null;
        public XElement xColumn = null;
        public XElement xRefTable = null;
        private int windowNumber;

        public DBQ(int windowNumber)
        {
            // TODO: Complete member initialization
            this.windowNumber = windowNumber;
        }

public    DBQ()
    {
        windowLookups = new Dictionary<int, List<MLookup>>();
        // TODO: Complete member initialization
    }

        //public bool LoadLookupTables()
        //{
        //    try
        //    {
        //        xRefTable = XElement.Parse(ExecuteString("Select AD_Table_ID as AD_TABLE_ID,IsValueDisplayed as ISVALUEDISPLAYED,WhereClause as WHERECLAUSE ,OrderByClause as ORDERBYCLAUSE, COLUMN_KEY_ID as COLUMN_KEY_ID, Column_Display_ID as COLUMN_DISPLAY_ID,AD_Reference_ID as AD_REFERENCE_ID ,IsActive as ISACTIVE From AD_Ref_Table where IsActive='Y'"));

        //        xColumn = XElement.Parse(ExecuteString("Select AD_TABLE_ID as AD_TABLE_ID, AD_Column_ID as AD_COLUMN_ID,ColumnName as COLUMNNAME, IsTranslated as ISTRANSLATED, AD_Reference_ID as AD_REFERENCE_ID, AD_Reference_Value_ID as AD_REFERENCE_VALUE_ID, IsIdentifier as ISIDENTIFIER,SeqNo as SEQNO From AD_Column"));


        //        xTable = XElement.Parse(ExecuteString("Select AD_Table_ID as AD_TABLE_ID,TableName as TABLENAME, AD_Window_ID as AD_WINDOW_ID,PO_Window_ID as PO_WINDOW_ID,IsActive as ISACTIVE From AD_Table"));
        //        return true;
        //    }
        //    catch
        //    {
        //        return false;
        //    }
        //}


        public static DBQ Get()
        {
            if (_dbq == null)
            {
                _dbq = new DBQ();
            }
            return _dbq;
        }


        public void AddWindowLookup(int windowNum, MLookup lookup)
        {
            if (windowLookups.ContainsKey(windowNum))
            {
                windowLookups[windowNum].Add(lookup);
            }
            else
            {
                List<MLookup> lst = new List<MLookup>();
                lst.Add(lookup);
                windowLookups.Add(windowNum, lst);
            }

        }


        public List<MLookup>  GetWindowLookup(int windowNum)
        {
            if (windowLookups.ContainsKey(windowNum))
            {
             return    windowLookups[windowNum];
            }
            return null;
        }




        private Dictionary<int, List<MLookup>> windowLookups = null;
        private static DBQ  _dbq = null;


        internal void Dispose(int windowNum)
        {
            List<MLookup> lst = null;
            if (windowLookups.TryGetValue(windowNum,out lst))
            {
                windowLookups.Remove(windowNum);
                lst.Clear();
                lst = null;
            }
        }

        internal void Dispose()
        {
            if (windowLookups != null)
            {
                windowLookups.Clear();
            }
            windowLookups = null;
        }



    }
}
